from pathlib import Path
from scripts.utils import get_configs
import os

def fix_rustversion_errors(proj_path: Path, CONFIG) :
    '''由于各种乱七八糟的版本错误，经过测试需要使用特定版本的rust才能运行c2rust编译出来的东西'''

    rust_config_file = f'{proj_path}/rust-toolchain'
    rust_config_file_with_toml = f'{proj_path}/rust-toolchain.toml'

    if os.path.exists(rust_config_file):
        os.remove(rust_config_file)

    if os.path.exists(rust_config_file_with_toml):
        os.remove(rust_config_file_with_toml)

    rustc_version = CONFIG['rust_version']
    with open(f'{proj_path}/rust-toolchain.toml', 'w', encoding='utf-8') as file:
        file.write(f'''[toolchain]
channel = "{rustc_version}"
components = ["rustfmt-preview", "rustc-dev", "rust-src", "miri", "rust-analyzer"]''')
        
#     with open(f'{proj_path}/rust-toolchain.toml', 'w', encoding='utf-8') as file:
#         file.write(f'''[toolchain]
# channel = "nightly-2022-08-08"
# components = ["rustfmt-preview", "rustc-dev", "rust-src", "miri", "rust-analyzer"]
# ''')
        
